﻿namespace Cos.Core.NHibernate.Util
{
    using Cos.Core.Model.Content;

    public static class CMSModelConverter
    {
        public static Nav ToNav(this NavDataModel navDataModel)
        {
            var nav = new Nav
                          {
                              NavId = navDataModel.Id,
                              InheritPermissions = navDataModel.InheritPermissions,
                              NavOrder = navDataModel.NavOrder,
                              ParentNavId = navDataModel.ParentId,
                              TemplateId = navDataModel.TemplateId
                          };

            return nav;
        }

        public static NavDataModel ToNavDataModel( this Nav nav)
        {
            var navDataModel = new NavDataModel
                                   {
                                       Id = nav.NavId,
                                       InheritPermissions = nav.InheritPermissions,
                                       NavOrder = nav.NavOrder,
                                       ParentId = nav.ParentNavId,
                                       TemplateId = nav.TemplateId
                                   };

            return navDataModel;
        }

        public static NavDataModel ToNavDataModel(this NavDataModel navDataModel, Nav nav)
        {
            if (navDataModel == null)
            {
                return ToNavDataModel(nav);
            }

            navDataModel.InheritPermissions = nav.InheritPermissions;
            navDataModel.NavOrder = nav.NavOrder;
            navDataModel.ParentId = nav.ParentNavId;
            navDataModel.TemplateId = nav.TemplateId;

            return navDataModel;
        }

        public static Page ToPage(this PageDataModel pageDataModel)
        {
            var page = new Page
                           {
                               NavId = pageDataModel.NavId,
                               PageId = pageDataModel.Id,
                               CultureCode = pageDataModel.CultureCode,
                               ApprobationDate = pageDataModel.ApprobationDate,
                               Author = pageDataModel.Author,
                               CreationDate = pageDataModel.CreationDate,
                               LastModified = pageDataModel.LastModified,
                               PageName = pageDataModel.PageName,
                               PageNamePreview = pageDataModel.PageNamePreview,
                               PageNavEnd = pageDataModel.PageNavEnd,
                               PageStatus = pageDataModel.PageStatus
                           };

            page.PageVisibility = false;
            if (pageDataModel.PageVisibility.HasValue)
            {
                page.PageVisibility = pageDataModel.PageVisibility.Value;
            }

            page.PageVisibilityPreview = false;
            if (pageDataModel.PageVisibilityPreview.HasValue)
            {
                page.PageVisibilityPreview = pageDataModel.PageVisibilityPreview.Value;
            }
            
            return page;
        }

        public static PageDataModel ToPageDataModel( this Page page)
        {
            var pageDataModel = new PageDataModel()
                        {
                            ApprobationDate = page.ApprobationDate,
                            Author = page.Author,
                            CreationDate = page.CreationDate,
                            CultureCode = page.CultureCode,
                            Id = page.PageId,
                            LastModified = page.LastModified,
                            NavId = page.NavId,
                            PageName = page.PageName,
                            PageNamePreview = page.PageNamePreview,
                            PageNavEnd = page.PageNavEnd,
                            PageStatus = page.PageStatus,
                            PageVisibility = page.PageVisibility,
                            PageVisibilityPreview = page.PageVisibilityPreview
                        };

            return pageDataModel;
        }

        public static PageDataModel ToPageDataModel(this PageDataModel pageDataModel, Page page)
        {
            if (pageDataModel == null)
            {
                return ToPageDataModel(page);
            }

            pageDataModel.ApprobationDate = page.ApprobationDate;
            pageDataModel.Author = page.Author;
            pageDataModel.CreationDate = page.CreationDate;
            pageDataModel.CultureCode = page.CultureCode;
            pageDataModel.LastModified = page.LastModified;
            pageDataModel.NavId = page.NavId;
            pageDataModel.PageName = page.PageName;
            pageDataModel.PageNamePreview = page.PageNamePreview;
            pageDataModel.PageNavEnd = page.PageNavEnd;
            pageDataModel.PageStatus = page.PageStatus;
            pageDataModel.PageVisibility = page.PageVisibility;
            pageDataModel.PageVisibilityPreview = page.PageVisibilityPreview;

            return pageDataModel;
        }

        public static Element ToElement(this ElementDataModel elementDataModel)
        {
            var element = new Element
                              {
                                  ElementId = elementDataModel.Id,
                                  PartId = elementDataModel.PartId,
                                  PageId = elementDataModel.PageId,
                                  Value = elementDataModel.ElementValue,
                                  PreviewValue = elementDataModel.ElementValuePreview,
                                  File = elementDataModel.ElementFile,
                                  PreviewFile = elementDataModel.ElementFilePreview
                              };

            return element;
        }

        public static ElementDataModel ToElementDataModel(this Element element)
        {
            var elementDataModel = new ElementDataModel
                                       {
                                           ElementFile = element.File,
                                           ElementFilePreview = element.PreviewFile,
                                           ElementValue = element.Value,
                                           ElementValuePreview = element.PreviewValue,
                                           Id = element.ElementId,
                                           PageId = element.PageId,
                                           PartId = element.PartId
                                       };

            return elementDataModel;
        }

        public static ElementDataModel ToElementDataModel(this ElementDataModel elementDataModel, Element element)
        {
            if (elementDataModel == null)
            {
                return ToElementDataModel(element);
            }

            elementDataModel.ElementFile = element.File;
            elementDataModel.ElementFilePreview = element.PreviewFile;
            elementDataModel.ElementValue = element.Value;
            elementDataModel.ElementValuePreview = element.PreviewValue;
            elementDataModel.PageId = element.PageId;
            elementDataModel.PartId = element.PartId;

            return elementDataModel;
        }
    }
}
